<!-- 部门管理 -->
<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button v-if="$hasPermission('crm:personnel:dept:add')" type="primary" plain @click="$router.push('/crm/dept/dept_add')">新增</el-button>
      </el-col>
    </el-row>
  </el-form>

  <div class="inner-container">
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column prop="id" label="部门编号"></el-table-column>
      <el-table-column prop="name" label="部门名称"></el-table-column>
      <el-table-column prop="parentId" label="上级部门"
                       :formatter="deptNameFormatter"></el-table-column>
      <el-table-column prop="deptHead" label="部门负责人"></el-table-column>
      <el-table-column prop="peopleNum" label="部门人数"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <el-button v-if="$hasPermission('crm:personnel:dept:updata')" type="primary" link
                     @click="goToDeptStatusView(scope.row)">编辑</el-button>
          <el-button v-if="$hasPermission('crm:personnel:dept:del')" type="primary" link
                     @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>


</template>

<script setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import request from "@/utils/request";
import {useRouter} from "vue-router";

const router = useRouter()
//定义加载方法
const loading = ref(false)
//引用
const queryFormRef = ref()


// 定义查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  id: undefined,
  userId: undefined,
  parentId: undefined,
  name: undefined,
  level: undefined,
  peopleNum: undefined,
  deptHead: undefined,
  createTime: undefined
})
//定义页码数
const total = ref(0)
//定义数组用来保存表格数据
const tableData = ref([])
const selectedIds = ref([])


//测试数据
const listData = () => {
  loading.value = true
  request.get('/api/crm/personnel/dept/select', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}


//定义删除的方法
const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/crm/personnel/dept/delete?ids=`+ids)
        .then(res => {
          selectedIds.value.length = 0
          listData()
          ElMessage.success('删除成功')
        })
  }).catch(() => {})
}

//跳转编辑
const goToDeptStatusView = (row) => {
  let id = row.id || selectedIds.value
  router.push({path: '/crm/dept/dept_update', query: {id: id}})
}

//获取部门名称字典
const deptNameArr = ref([])
onMounted(()=>{
  loadDictOptions(deptNameArr,'dept_tall_name');
})
const  deptNameFormatter = (row,column,cellValue,index)=>{
  if(!deptNameArr.value){
    return cellValue;
  }
  for(let item of deptNameArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}


//处理选择更改
const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}


//处理页码数变化
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

//处理每页条数变化
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

//一进页面就获取数据
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
